package com.tzstcl.framework.handler;

import com.tzstcl.framework.shiro.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.util.Map;

/**
 * webSocket握手处理器
 **/
public class WebSocketHandshakeHandler extends DefaultHandshakeHandler {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketHandshakeHandler.class);

    /**
     * 为webSocket连接声明用户信息
     **/
    @Override
    public Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
        return new Principal(){
            private String name = ShiroUtils.getUser().getId().toString();
            @Override
            public String getName() {
                return name;
            }
        };
    }

}
